-- gem
-- created by panyl
-- 钻石

-- 怪物死亡事件
EventMgr.register("gift_gem", event.COST_ATTRIB, function(para)
    local gem = para["gem"];
    local money = para["money"];

    if not gem and not money then
        -- 如果不是消耗钻石
        return;
    end

    local progress = ME.user.dbase:query("gift_progress", {});

    local p = progress["gem"] or 0;
    local m = progress["money"] or 0;
    if gem then
        progress["gem"] = p + gem;
    end
    if money then
        progress["money"] = m + money;
        if progress["money"] >= 50000 then
            progress["gem"] = p + progress["money"] / 50000;
            progress["money"] = progress["money"] % 50000;
        end
    end

    ME.user.dbase:set("gift_progress", progress);
end);

return {
    getProgress = function()
        -- 已消费的钻石
        local progress = ME.user.dbase:query("gift_progress", {});

        return progress["gem"] or 0;
    end,

    checkProgress = function(grade)
        -- 已消费的钻石
        local progress = ME.user.dbase:query("gift_progress", {});
        local p = progress["gem"] or 0;

        local config = GiftM.query("gem", grade);

        return p >= config["require"];
    end,
};
